package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartcanteen.mapper.GoodsMapper;
import com.example.smartcanteen.mapper.ShopMapper;
import com.example.smartcanteen.model.Goods;
import com.example.smartcanteen.model.Shop;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ShopMapper shopMapper;


    //上架商品
    public ResponseMessage grounding(Goods goods) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();

        //首先判断店铺id是否存在
        shopQueryWrapper.eq("id", goods.getShopID());
        if (shopMapper.selectOne(shopQueryWrapper) != null) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();

            //其次判断同一店铺内商品是否重复：根据商品名判断
            goodsQueryWrapper.eq("goodsname", goods.getGoodsname());
            if (goodsMapper.selectOne(goodsQueryWrapper) == null) {
                goodsMapper.insert(goods);
                responseMessage.setSuccess(true);
            } else {
                responseMessage.setSuccess(false);
            }
        } else {
            responseMessage.setSuccess(false);
        }
        return responseMessage;
    }


    //查看所有商品（管理员适用）
    public ResponseMessage selectAll() {
        ResponseMessage responseMessage = new ResponseMessage();

        //根据商家ID搜索指定店铺内所以商品
//        QueryWrapper<Goods> goodsQueryWrapper=new QueryWrapper<>();
//        goodsQueryWrapper.eq("shopID",shopID);
        responseMessage.setData(goodsMapper.selectList(null));
        return responseMessage;
    }


    //查看店内所有商品（学生适用）
    public ResponseMessage selectAllSG(Integer shopID){
        ResponseMessage responseMessage = new ResponseMessage();

//        根据商家ID搜索指定店铺内所以商品
        QueryWrapper<Goods> goodsQueryWrapper=new QueryWrapper<>();
        goodsQueryWrapper.eq("shopID",shopID)
                .eq("state","在售");
        responseMessage.setData(goodsMapper.selectList(goodsQueryWrapper));
        return responseMessage;
    }


    //查看店内所有商品（学生适用）
    public ResponseMessage selectAllSSG(Integer shopID){
        ResponseMessage responseMessage = new ResponseMessage();

//        根据商家ID搜索指定店铺内所以商品
        QueryWrapper<Goods> goodsQueryWrapper=new QueryWrapper<>();
        goodsQueryWrapper.eq("shopID",shopID);
        responseMessage.setData(goodsMapper.selectList(goodsQueryWrapper));
        return responseMessage;
    }
}
